package com.nyist.myticket.core.factory;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import com.nyist.myticket.core.base.ApplicationContextFactory;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.NoSuchObjectException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 * @author yangxiaogen
 * @date 2020/12/30 14:53
 */
public class GenerateCodeFactory {

    public static final String GAODE_KEY = "1a16983381c4f912063cb602a7e43c3c";

    private static final Lock LOCK = new ReentrantLock();
    private static volatile  int lastCount = 1;
    private static int count = 0;
    private static final String first = "10";
    //10+yyyymmdd+八位序列

    //驿站信息
    public static final String CODE_PREFIX_SiteId = "118";
    // 定制化版本表ID
    public static final String CODE_PREFIX_oemVersionId = "119";

    /**
     * 序列号名称
     */
    public static final String SEGMENT_NAME_deviceLogId= "deviceLogId";

    /**
     * 只有在不调用服务生成ID时有用
     */
    private static Map<String, String> prefixMap = null;

    /**
     * 本机ip地址
     */
    //private static final String ipv4 = new InetUtils(new InetUtilsProperties()).findFirstNonLoopbackAddress().getHostAddress();

    private static String ipv4;

    static {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            ipv4=localHost.toString();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    /**
     * 当前节点编号
     */
    private static  String nodeCode = "00";

    static {
        prefixMap = new HashMap<String, String>();
        //10+yyyymmdd+八位序列
        prefixMap.put("oId", "10");
        //（20+yyyymmdd+八位序列）
        prefixMap.put("bId", "20");
        //（11+yyyymmdd+八位序列）
        nodeCode =  String.format("%02d", NetUtil.ipv4ToLong(ipv4)%99);
        System.out.println("ipv4ToLong="+ NetUtil.ipv4ToLong(ipv4));
        System.out.println("nodeNum="+nodeCode);
    }

    private static String PLATFORM_CODE = "0001";

    private static long workerId = NetUtil.ipv4ToLong(ipv4)%31;
    private static Snowflake snowflake = IdUtil.createSnowflake(workerId, 1);

    public static Long snowflakeId() {
        long id = 1L;
        LOCK.lock();
        try {
            id = snowflake.nextId();
        } finally {
            LOCK.unlock();
        }
        return id;
    }



    /**
     * 生成dataFlowId
     *
     * @return
     * @throws
     */
    public static String getDataFlowId()  {

        return UUID.randomUUID().toString().replace("-", "").toLowerCase();

    }

    /**
     * 获取restTemplate
     *
     * @return
     * @throws NoSuchObjectException
     */
    private static RestTemplate restTemplate() throws NoSuchObjectException {

        Object bean = ApplicationContextFactory.getBean("restTemplate");

        if (bean == null) {
            throw new NoSuchObjectException("没有找到restTemplate对象，请核实");
        }

        return (RestTemplate) bean;
    }

    private static RestTemplate outRestTemplate() throws NoSuchObjectException {

        Object bean = ApplicationContextFactory.getBean("outRestTemplate");

        if (bean == null) {
            throw new NoSuchObjectException("没有找到restTemplate对象，请核实");
        }

        return (RestTemplate) bean;
    }

    /**
     * 获取随机数
     *
     * @return
     */
    private static String getRandom() {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 4; i++) {
            result += random.nextInt(10);
        }
        return result;
    }

    /**
     * 获取随机数
     *
     * @return
     */
    public static String getRandomCode(int bit) {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < bit; i++) {
            result += random.nextInt(10);
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(snowflakeId());
    }
}
